Imports System.Windows.Forms

Public Class ManageCategories

  Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
    Dim _Categories As New System.Text.StringBuilder
    Dim TempCat As String

    For Each x As Object In Me.CategoriesListBox.Items
      TempCat = x
      TempCat = TempCat.Replace(",", "_")

      If _Categories.Length > 0 Then
        _Categories.Append(",")
        _Categories.Append(TempCat)
      Else
        _Categories.Append(TempCat)
      End If
    Next

    My.Settings.Categories = _Categories.ToString
    My.Settings.Save()

    Me.DialogResult = System.Windows.Forms.DialogResult.OK
    Me.Close()
  End Sub

  Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
    Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
    Me.Close()
  End Sub

  Private Sub ManageCategories_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    My.Settings.ManageCategoriesDialogLocation = Me.Location
    My.Settings.ManageCategoriesDialogSize = Me.Size
    My.Settings.Save()
  End Sub

  Private Sub ManageCategories_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If My.Settings.ManageCategoriesDialogLocation.Height > -1 Then
      Me.Location = My.Settings.ManageCategoriesDialogLocation
    End If

    If My.Settings.ManageCategoriesDialogSize.Height > -1 Then
      Me.Size = My.Settings.ManageCategoriesDialogSize
    End If

    LoadCategories()
  End Sub

  Private Sub CategoriesListBox_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles CategoriesListBox.KeyUp

    Select Case e.KeyCode
      Case Keys.Delete
        DeleteCategory()
      Case Keys.Insert
        AddCategory()
    End Select

  End Sub

  Private Sub LoadCategories()
    Me.CategoriesListBox.Items.Clear()

    Try
      If My.Settings.Categories.Length > 0 Then
        Dim x() As String = My.Settings.Categories.Split(",")
        Array.Sort(x)
        Me.CategoriesListBox.Items.AddRange(x)
      End If

    Catch ex As Exception

    End Try

  End Sub

  Private Sub AddCategory()
    Dim TempCat As String = String.Empty
    TempCat = InputBox("Please enter the new Category", "Insert/Add New Category")
    If TempCat.Length > 0 Then
      Me.CategoriesListBox.Items.Add(TempCat.Replace(",", "_"))
    End If

  End Sub

  Private Sub DeleteCategory()
    If CategoriesListBox.SelectedIndex > -1 Then
      CategoriesListBox.Items.RemoveAt(CategoriesListBox.SelectedIndex)
    End If
  End Sub

  Private Sub AddLinkLabel_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles AddLinkLabel.LinkClicked
    AddCategory()
  End Sub

  Private Sub DeleteLinkLabel_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles DeleteLinkLabel.LinkClicked
    DeleteCategory()
  End Sub
End Class
